package com.gdqy.FCS.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gdqy.FCS.common.api.FastResponse;
import com.gdqy.FCS.common.api.ListResponse;
import com.gdqy.FCS.common.mvc.Args;
import com.gdqy.FCS.entity.Demand;
import com.gdqy.FCS.service.DemandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@Controller
@RequestMapping("demand")
public class DemandController {
	@Autowired
	private DemandService demandService;
	
	
	//获取购买需求-----测试完毕
	
	/**
	 * 
	 *作者：张伟键
	 *方法描述：
	 * @param designer_id 设计师id
	 * @param args
	 * @param pageindex 当前页面
	 * @param pagesize 页面数据容量
	 * @return
	 */
		@RequestMapping("getDemand")
		public ModelAndView getDemand(@RequestParam(value = "designer_id", required = true) int designer_id,
				Args args,int pageindex, int pagesize){
			ModelAndView modelAndView = new ModelAndView();
			PageHelper.startPage(pageindex, pagesize);
			List<Demand> demands =  demandService.selectByDesigner_id(designer_id);
			
			PageInfo<Demand> page = new PageInfo<Demand>(demands);
			 ListResponse<Demand> json= FastResponse._().errCode(0).subCode(0).errMsg("获取购买需求成功")
					.args(args.getReqeustargs()).pageIndex(page.getPageNum())
					.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
					.hasNext(page.isHasNextPage()).list(demands);
			 System.out.println(json.getList().size());
			 modelAndView.addObject("json", json);
			 modelAndView.setViewName("designer/demand");
			 return modelAndView;
		}
		
		
		//删除购买需求------测试完毕
		@RequestMapping("deleteDemand")
		public @ResponseBody Object deleteDemand(int id,HttpSession session){
			String msg="fail";
			//判断该需求是否问该用户所有
				int session_uid = (int) session.getAttribute("designer_id");
				Demand demand = demandService.selectById(id);
				if(session_uid == demand.getDesigner_id()){
					int data = demandService.deleteById(id);
					if(data>0){
						msg="success";
					}
				}
			
			return FastResponse._().obj(msg);
			
		}
		
		//添加购买需求
		@RequestMapping("add_demand")
		public @ResponseBody String add_demand(Demand demand){
			String msg = "fail";
			demand.setDatetime(new Date());
			int data = demandService.insert(demand);
			if(data>0){
				msg="success";
			}
			return msg;
			
		}
}
